//package com.app.config;
//
///**
// * Created by lijun on 2018/2/11.
// */
//
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Set;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import com.app.entity.authority.User;
//import com.fasterxml.jackson.databind.ObjectMapper;
//
//import org.springframework.security.core.Authentication;
//import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
//import org.springframework.stereotype.Component;
//
//@Component
//public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
//    @Override
//    public void onAuthenticationSuccess(HttpServletRequest request,
//                                        HttpServletResponse response, Authentication authentication) throws IOException,
//            ServletException {
//        //获得授权后可得到用户信息   可使用SUserService进行数据库操作
//        User userDetails = (User)authentication.getPrincipal();
//        /* Set<SysRole> roles = userDetails.getSysRoles();*/
//        //输出登录提示信息
//        System.out.println("管理员 " + userDetails.getName() + " 登录");
//
//        System.out.println("IP :"+getIpAddress(request));
//        //保护用户信息安全，密码不能传到前端去
//        User user = new User();
//        user.setLoginname(userDetails.getName());
//        user.setName(userDetails.getLoginname());
//        user.setId(userDetails.getId());
//
//        Map<String, Object> results = new HashMap<String,Object>();
//        results.put("code", 200);
//        results.put("message", "操作成功！");
//        results.put("data", user);
//        response.setContentType("application/json; charset=utf-8");
//        response.getWriter().print(new ObjectMapper().writeValueAsString(results));
//
//
//        //super.onAuthenticationSuccess(request, response, authentication);
//    }
//
//    public String getIpAddress(HttpServletRequest request){
//        String ip = request.getHeader("x-forwarded-for");
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("WL-Proxy-Client-IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_CLIENT_IP");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//        }
//        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
//            ip = request.getRemoteAddr();
//        }
//        return ip;
//    }
//}


package com.app.config;

/**
 * Created by lijun on 2018/2/11.
 */

import com.app.entity.authority.SecurityUser;
import com.app.entity.authority.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response, Authentication authentication) throws IOException,
            ServletException {


       // super.onAuthenticationSuccess(request, response, authentication);


        //获得授权后可得到用户信息   可使用SUserService进行数据库操作
        SecurityUser userDetails = (SecurityUser)authentication.getPrincipal();
        /* Set<SysRole> roles = userDetails.getSysRoles();*/
        //输出登录提示信息
      //  System.out.println("管理员 " + userDetails.getName() + " 登录");

      //  System.out.println("IP :"+getIpAddress(request));
        //保护用户信息安全，密码不能传到前端去
//        User user = new User();
//        user.setLoginname(userDetails.getName());
//        user.setName(userDetails.getLoginname());
//        user.setId(userDetails.getId());

        Map<String, Object> results = new HashMap<String,Object>();
        results.put("code", 200);
        results.put("message", "操作成功！");
        results.put("data", userDetails);
      //  results.put("token",getArray(userDetails.roles));
        results.put("token",StringUtils.join(userDetails.getRolesArray(), ","));

        response.setContentType("application/json; charset=utf-8");
        response.getWriter().print(new ObjectMapper().writeValueAsString(results));


    }
  /*  String getArray(String[] array){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<array.length;i++){
            sb.append(array[i]);
            sb.append(",");
        }

        String result=sb.toString();
        if(result.endsWith(","))
            result=result.substring(0,result.length()-1);

        return  result;
    }*/

    public String getIpAddress(HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}